package com.yss.reportworld.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import com.yss.reportworld.domain.Customer;
import com.yss.reportworld.service.ICustomerService;
import com.yss.reportworld.service.IWeComService;
import com.yss.system.api.RemoteConfigService;
import lombok.extern.slf4j.Slf4j;
import com.yss.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.reportworld.service.IRecordService;
import com.yss.reportworld.mapper.RecordMapper;
import com.yss.reportworld.domain.Record;


/**
 * 版本记录信息Service业务层处理
 *
 * @author yss
 * @date 2023-07-06
 */
@Slf4j
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private IWeComService weComService;
    /*  @Resource
      private JavaMailSender javaMailSender;*/
    @Autowired
    private ICustomerService customerService;
    // 系统配置发送邮箱发件人和名称，字符集
 /*   @Value("${spring.mail.properties.display.sendmail}")
    private String sendmail;
    @Value("${spring.mail.properties.display.sendname}")
    private String sendName;
    @Value("${spring.mail.properties.mail.default-encoding}")
    private String encoding;
    // 报表模块单独配置的发送邮件信息
    @Value("${spring.mail.send}")
    private boolean send;
    @Value("${spring.mail.sendto}")
    private String sendTo;
    @Value("${spring.mail.sendcc}")
    private String sendCc;
*/

    /**
     * 查询版本记录信息列表
     *
     * @param record 版本记录信息
     * @return 版本记录信息
     */
    @Override
    public List<Record> selectRecordList(Record record) {
        QueryWrapper<Record> wrapper = new QueryWrapper();
        if (StringUtils.isNull(record)) {
            return this.list(wrapper);
        }
        if (StringUtils.isNotEmpty(record.getId())) {
            wrapper.eq("a.ID", record.getId());
        }
        if (StringUtils.isNotEmpty(record.getStrCustomer())) {
            wrapper.in("a.CUSTOMER_ID", record.getStrCustomer());
        }
        if (StringUtils.isNotEmpty(record.getParams())) {
            if (null != record.getParams().get("beginTime") && StringUtils.isNotEmpty(record.getParams().get("beginTime").toString())) {
                wrapper.ge("a.CREATE_TIME", record.getParams().get("beginTime"));
            }
            if (null != record.getParams().get("endTime") && StringUtils.isNotEmpty(record.getParams().get("endTime").toString())) {
                wrapper.le("a.CREATE_TIME", record.getParams().get("endTime"));
            }
        }
        wrapper.orderByDesc("a.CREATE_TIME");
        // 导出的时候选择了数据，按照选择的数据
        if (StringUtils.isNotEmpty(record.getIds())) {
            List<String> list = JSONArray.parseArray(record.getIds(), String.class);
            if(StringUtils.isNotEmpty(list)) {
                wrapper.in("a.ID", list);
            }
        }
        return recordMapper.queryRecord(wrapper);
    }

    /**
     * 新增版本记录信息
     *
     * @param record
     */
    public void saveRecord(Record record) {
        record.setId(IdUtils.fastSimpleUUID());
        record.setCreateBy(SecurityUtils.getUsername());
        record.setCreateTime(new Date());
        this.save(record);
     /*   if (!send || StringUtils.isEmpty(sendmail) || StringUtils.isEmpty(sendName)
                || StringUtils.isEmpty(sendTo) || StringUtils.isEmpty(record.getCustomerId())) {
            return;
        }*/
        Customer customer = customerService.getById(record.getCustomerId());
        if (StringUtils.isNull(customer) || StringUtils.isEmpty(customer.getName())) {
            log.error("没有获取到客户信息！");
            return;
        }
        try {
      /*      MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 设置utf-8或GBK编码，否则邮件会有乱码
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, encoding);
            messageHelper.setFrom(sendmail, sendName);
            messageHelper.setTo(Convert.toStrArray(sendTo));
            if (StringUtils.isNotEmpty(sendCc)) {
                messageHelper.setCc(Convert.toStrArray(sendCc));
            }
            messageHelper.setSubject("版本记录信息");*/
            StringBuffer buffer = new StringBuffer();
            buffer.append(SecurityUtils.getLoginUser().getSysUser().getNickName());
            buffer.append("提交").append(customer.getName()).append("版本记录：\n");
            buffer.append("   版本记录：\n");
            buffer.append((StringUtils.isNotEmpty(record.getDescription()) ?
                    "      文件信息：" + record.getDescription() : "") + "\n");
            buffer.append("      说明：" + record.getDescription() + "\n");
            buffer.append((StringUtils.isNotEmpty(record.getRemark()) ? "      备注：" + record.getRemark() : ""));
            //messageHelper.setText(buffer.toString(), false);
            // javaMailSender.send(mimeMessage);
            weComService.sendMessageToManage(buffer.toString());
        } catch (Exception ex) {
            log.error("发送邮件失败" + ex.getMessage());
        }
    }
}
